package com.google.android.apps.mytracks.investigados;

import com.google.android.apps.mytracks.content.MyTracksProvider.DatabaseHelper;
import com.google.android.apps.mytracks.content.TelefonoInvestigado;
import com.google.android.apps.mytracks.content.TelefonosInvestigadoColumns;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

public class TelefonoInvestigadoDataSource {

  // Database fields
  private SQLiteDatabase database;
  private DatabaseHelper dbHelper;
  private String[] allColumns = TelefonosInvestigadoColumns.COLUMNS;

  public TelefonoInvestigadoDataSource(Context context) {
    dbHelper = new DatabaseHelper(context);
  }

  public void open() throws SQLException {
    database = dbHelper.getWritableDatabase();
  }

  public void close() {
    dbHelper.close();
  }

  public long createTelefonoInvestigado(TelefonoInvestigado tf) {
    ContentValues values = new ContentValues();
    values.put(TelefonosInvestigadoColumns.INVESTIGADOID, tf.getInvestigadoId());
    values.put(TelefonosInvestigadoColumns.NUMERO, tf.getNumero());
    values.put(TelefonosInvestigadoColumns.TIPO, tf.getTipo());
    long insertId = database.insert(TelefonosInvestigadoColumns.TABLE_NAME, null, values);
    return insertId;
  }

  public void deleteTelefonoInvestigado(TelefonoInvestigado telefonoInvestigado) {
    long id = telefonoInvestigado.getId();
    database.delete(TelefonosInvestigadoColumns.TABLE_NAME, TelefonosInvestigadoColumns._ID
        + " = " + id, null);
  }
  
  public void updateTelefonoInvestigado(TelefonoInvestigado telefonoInvestigado) {
    
    long id = telefonoInvestigado.getId();
    ContentValues values = new ContentValues();
    values.put(TelefonosInvestigadoColumns.NUMERO, telefonoInvestigado.getNumero());
    values.put(TelefonosInvestigadoColumns.TIPO, telefonoInvestigado.getTipo());
    String strFilter = "_id=" + id;
    database.update(TelefonosInvestigadoColumns.TABLE_NAME, values, strFilter, null);
  }

  public List<TelefonoInvestigado> getAllTelefonoInvestigado(long investigadoId) {
    ArrayList<TelefonoInvestigado> telefonosInvestigado = new ArrayList<TelefonoInvestigado>();
    String[] args = new String[] {String.valueOf(investigadoId)};
    Cursor cursor = database.query(TelefonosInvestigadoColumns.TABLE_NAME,
        allColumns, "investigadoId=?", args, null, null, TelefonosInvestigadoColumns.DEFAULT_SORT_ORDER);

    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
      TelefonoInvestigado telefonoInvestigado = cursorToTelefonoInvestigado(cursor);
      telefonosInvestigado.add(telefonoInvestigado);
      cursor.moveToNext();
    }
    cursor.close();
    return telefonosInvestigado;
  }

  private TelefonoInvestigado cursorToTelefonoInvestigado(Cursor cursor) {
    TelefonoInvestigado telefonoInvestigado = new TelefonoInvestigado();
    telefonoInvestigado.setId(cursor.getLong(0));
    telefonoInvestigado.setInvestigadoId(cursor.getLong(1));
    telefonoInvestigado.setNumero(cursor.getString(2));
    telefonoInvestigado.setTipo(cursor.getString(3));
    return telefonoInvestigado;
  }
} 